<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Information.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Menusinfo.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Products.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Page.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/News.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/FriendLink.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/FeedBack.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Jobs.php";

/**
 * 静态化工具类
 * Class HtmlOperation
 */
class HtmlOperation
{
    private $pageSize = 20;

    /**
     * 全站静态化
     */
    function all()
    {
        $this->index();
        $this->product();
        $this->news();
        $this->page();
        //$this->feedback();
        // $this->jobs();
        //   $this->friend_link();
        $this->sitemap();
    }

    /**
     * 生成首页文件
     */
    function index()
    {
        $this->html("", "index.php", "index.html");
    }

    /**
     * @param $data 数据
     * @param $file_name 模板文件名
     * @param $html_name 静态文件名
     */
    function html($data, $file_name, $html_name, $dir_name = "")
    {
        $html_data = $data;
        ob_start();
        $is_html = true;
        require($_SERVER['DOCUMENT_ROOT'] . "/view/$file_name");
        $dir_name = $_SERVER['DOCUMENT_ROOT'] . ($file_name === "index.php" ? $dir_name : "/html/$dir_name");
        if (!is_dir($dir_name)) {
            mkdir($dir_name);
        }
        $content = ob_get_contents();
        if (file_put_contents("$dir_name/$html_name", $content)) {
            error_log("生成文件$html_name 完成");
            ob_end_clean();
            return true;
        } else {
            error_log("生成文件$html_name 失败");
            ob_end_clean();
            return false;
        }
//        return true;
    }

    private function loadListByType($type)
    {
        if (!$type) {
            die("请提供数据类型");
        }
        $menus = new Menusinfo();
        return $menus->loadDataByType($type);
    }

    function page()
    {
        $page = new Page();
        $datas = $page->findList(["is_del" => 0]);
        foreach ($datas as $val) {
            $id = $val["idDataCategory"];
            $_GET["id"] = $id;
            $this->html("", "page.php", "page$id.html", "page");
        }
    }

    /**
     * 生成产品页静态文件
     */
    function product()
    {

        $product = new Products();
        $redata = $this->loadListByType("products");
        error_log(json_encode($redata));
        $_GET["type"] = "product";
        foreach ($redata as $temp) {
            $catId = $temp["idMenuinfo"];
            $pdatas = $product->findByPage(1, $this->pageSize, ["id_data_category" => $catId, "is_del" => 0]);
            $_GET["id"] = $catId;
            $this->html("", "products.php", "products$catId.html", "products");
            $pages = (int)$pdatas["totalPages"];
            for ($i = 2; $i <= $pages; $i++) {
                $product->findByPage($i, $this->pageSize, ["id_data_category" => $catId]);
                $_GET["id"] = $catId;
                $this->html("", "products.php", "products$catId" . "$i.html", "products");
            }
        }
        $datas = $product->findList(["is_del" => 0]);
        foreach ($datas as $val) {
            $id = $val["idProduct"];
            $_GET["id"] = $id;
            $_GET["detail_id"] = $id;
            $this->html("", "product_detail.php", "product_detail$id.html", "products");
        }
    }

    /**
     * 生成新闻页静态文件
     */
    function news()
    {
        $news = new News();
        $redata = $this->loadListByType("news");
        foreach ($redata as $temp) {
            $catId = $temp["idMenuinfo"];
            $pdatas = $news->findByPage(1, $this->pageSize, ["id_data_category" => $catId, "is_del" => 0]);
            $_GET["id"] = $catId;
            $this->html("", "news.php", "news$catId.html", "news");
            $pages = (int)$pdatas["totalPages"];
            for ($i = 2; $i <= $pages; $i++) {
                $news->findByPage($i, $this->pageSize, ["id_data_category" => $catId]);
                $_GET["id"] = $catId;
                $this->html("", "news.php", "news$catId" . "$i.html", "news");
            }
        }

        $datas = $news->findList(["is_del" => 0]);
        foreach ($datas as $val) {
            $id = $val["idNews"];
            $_GET["id"] = $id;
            $_GET["detail_id"] = $id;
            $this->html("", "news_detail.php", "news_detail$id.html", "news");
        }
    }

    /**
     * 在线反馈
     */
    function feedback()
    {
        $feedback = new FeedBack();
        $datas = $feedback->findList(["is_del" => 0]);
        foreach ($datas as $val) {
            $id = $val["idFeedBack"];
            $_GET["id"] = $id;
            $this->html("", "feedback.php", "feedback$id.html", "news");
        }
    }

    /**
     * 在线招聘
     */
    function jobs()
    {
        $jobs = new Jobs();
        $datas = $jobs->findList(["is_del" => 0]);
        foreach ($datas as $val) {
            $id = $val["idJobs"];
            $_GET["id"] = $id;
            $this->html("", "job_detail.php", "job_detail$id.html", "news");
        }
    }

    /**
     * 友情链接
     */
    function friend_link()
    {
        $fl = new FriendLink();
        $datas = $fl->findList(["is_del" => 0]);
        foreach ($datas as $val) {
            $id = $val["idFriendLink"];
            $_GET["id"] = $id;
            $this->html("", "friend_link.php", "friend_link$id.html", "news");
        }
    }

    /**
     * 网站地图
     */
    function sitemap()
    {

    }
}